package com.yujistu.fragment.appointfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.yujistu.App;
import com.yujistu.R;
import com.yujistu.activity.EnterpriseItemActivity;
import com.yujistu.adapter.BaseListAdapter;
import com.yujistu.fragment.BaseFragment;
import com.yujistu.fragment.appointfragment.couseadapter.EnterpriseAdapter;
import com.yujistu.integration.request.EnterRequest;
import com.yujistu.integration.result.EnterResult;
import com.yujistu.network.StuRestClient;
import com.yujistu.utils.Logger;

import java.lang.reflect.Type;
import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Win on 2015/11/6.
 */
public class EnterpriseFragment extends BaseFragment<EnterResult> {

    View  mContent;
    ListView  mListView;
    EnterpriseAdapter    mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

       mContent = inflater.inflate(R.layout.fragment_enter_prise, container, false);

        return mContent;
    }

    @Override
    public void onResume() {
        super.onResume();
        /*mSrlayout = (SwipeRefreshLayout)mContent.findViewById(R.id.enterSRLayout);
        mSrlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });*/

        mListView = (ListView)mContent.findViewById(R.id.enterLV);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listRequest();
            }
        });
        mAdapter = new EnterpriseAdapter(mActivity);
        mListView.setAdapter(mAdapter);
    }

    private void listRequest() {
        EnterRequest request = new EnterRequest();
        Logger.i("request:" + request);
        request.token = App.getInstance().getToken();
        // request.page = urlPage;

        RequestParams  params = new RequestParams();

        StuRestClient.get(getActivity(), request.getHttpUrl(), params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                startActivity(new Intent(getActivity(), EnterpriseItemActivity.class));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

    @Override
    public BaseListAdapter getListAdapter() {
        return null;
    }

    @Override
    public ArrayList<EnterResult> getArrayList(String jsonStr) {
        Gson  gson = new Gson();
        Type listType = new TypeToken<ArrayList<EnterResult>>(){
        }.getType();
        ArrayList<EnterResult> list = gson.fromJson(jsonStr,listType);
        return  list;
    }

    @Override
    protected String getJsonArrayName() {
        return "courses";
    }

   /* @Override
    protected String getURL() {
        return EnterpriseHelper.ENTERPRISE_COURSE_LIST;
    }*/
}
